import Mock from 'mockjs'
import {baseUrl} from "../utils/global";
import * as login from './modules/login'
import * as user from './modules/user'
import * as dept from './modules/dept'
import * as role from './modules/role'
import * as menu from './modules/menu'
import * as dict from './modules/dict'

//开启/关闭【所有模块】拦截，通过调用[openMock参数]设置
//开启/关闭【业务模块】拦截，通过调用fnCreate方法[isOpen参数]设置
//开启/关闭【业务模块某个请求】拦截，通过函数返回对象中的[isOpen属性]设置
let openMock = false;
fnCreate(login, openMock);
fnCreate(user, openMock);
fnCreate(role, openMock);
fnCreate(menu, openMock);
fnCreate(dept, openMock);
fnCreate(dict, openMock);

/**
 * 创建Mock模拟数据
 * @param mod mod模块
 * @param isOpen 是否开启
 */
function fnCreate(mod, isOpen = true) {
  if (isOpen) {
    for (let key in mod) {
      ((res) => {
        if (res.isOpen !== false) {
          let url = baseUrl;
          if (!url.endsWith("/")) {
            url = url + "/";
          }
          url = url + res.url;
          Mock.mock(new RegExp(url), res, type, (opts) => {
            opts['data'] = opts.body ? JSON.parse(opts.body) : null;
            delete opts.body;
            console.log("\n");
            console.log('%mock拦截，请求：', 'color:blue', opts);
            console.log('%mock拦截，响应：', 'color:blue', res.data);
            return res.data;
          });
        }
      })(mod[key]() || {})
    }
  }
}
